Game xếp hình 2048

13.189 lượt xem;
1 using UnityEngine;
2 using
System.Collections;
3
4 public
class ConnectorScript : MonoBehaviour {
5
6     
public int x;
7     
public int y;
8     
public int z;
9     
public string axis; //x,y,z
10     
private GameControllerScript gameScript;
11     
private float scale;
12     
private float yOffset;
13     
private float rotationTotal = -1f;
14     
private Vector3 rotateDirection;
15     
private float moveDuration;
16     
// Use this for initialization
17     
void Start () {
18         
//setup local versions of Game Variables
19
20
21
22     }
23
24     
public void Initialize(int x, int y, int z, string axis, GameControllerScript gameScript) {
25         
this.scale = gameScript.scale;
26         
this.yOffset = gameScript.yOffset;
27         
this.gameScript = gameScript;
28         
this.moveDuration = gameScript.moveDuration;
29         
this.x = x;
30         
this.y = y;
31         
this.z = z;
32         
this.axis = axis;
33         
this.ResetPosition();
34     }
35
36     
private void ResetPosition() {
37         
38         
if (this.axis == "x") {
39             transform.position =
new Vector3(
40                 x *
this.scale + (this.scale/2f),
41                 y *
this.scale + this.yOffset,
42                 z *
this.scale);
43             transform.eulerAngles =
new Vector3(0,0,90);
44         }
45         
if (this.axis == "y") {
46             transform.position =
new Vector3(
47                 x *
this.scale,
48                 y *
this.scale + this.yOffset + this.scale/2f,
49                 z *
this.scale);
50             transform.eulerAngles =
new Vector3(0,0,0);
51         }
52         
if (this.axis == "z") {
53             transform.position =
new Vector3(
54                 x *
this.scale,
55                 y *
this.scale + this.yOffset,
56                 z *
this.scale + this.scale/2f);
57             transform.eulerAngles =
new Vector3(90,0,0);
58         }
59     }
60
61     
/*
62      * Method to determine
if the connector should be shown
63      */

64     
private bool show(){
65
66         
//if this block is one that should not be shown
67         
if (this.GetBlockNumber(this.x, this.y, this.z) == -2) return false;
68
69         
if (this.axis == "x") {
70             
//if this is on the edge then don't display
71             
if(this.x == 2) return false;
72             
//check if the neighboring block should not be shown
73             
if(this.GetBlockNumber (this.x + 1, this.y, this.z) == -2) return false;
74         }
75         
if (this.axis == "y") {
76             
//if this is on the edge then don't display
77             
if(this.y == 2) return false;
78             
//check if the neighboring block should not be shown
79             
if(this.GetBlockNumber (this.x, this.y+1, this.z) == -2) return false;
80         }
81         
if (this.axis == "z") {
82             
//if this is on the edge then don't display
83             
if(this.z == 2) return false;
84             
//check if the neighboring block should not be shown
85             
if(this.GetBlockNumber (this.x, this.y, this.z+1) == -2) return false;
86         }
87
88         
return true;
89     }
90
91     
private Color getConnectorColor(){
92         Color standardColor =
new Color(0,0.5f, 0);
93         Color highlightedColor =
new Color(0f, 0f, 1f);
94         
int block0;
95         
int block1;
96         
int block2;
97         
int blockPosition;
98
99         
if (this.axis == "x") {
100             blockPosition =
this.x;
101             block0 =
this.GetBlockNumber(0,this.y, this.z);
102             block1 =
this.GetBlockNumber(1,this.y, this.z);
103             block2 =
this.GetBlockNumber(2,this.y, this.z);
104         }
105         
else if (this.axis == "y") {
106             blockPosition =
this.y;
107             block0 =
this.GetBlockNumber(this.x,0, this.z);
108             block1 =
this.GetBlockNumber(this.x,1, this.z);
109             block2 =
this.GetBlockNumber(this.x,2, this.z);
110         }
111         
else if (this.axis == "z") {
112             blockPosition =
this.z;
113             block0 =
this.GetBlockNumber(this.x,this.y, 0);
114             block1 =
this.GetBlockNumber(this.x,this.y, 1);
115             block2 =
this.GetBlockNumber(this.x,this.y, 2);
116         }
117         
else { //not quite sure why axis isn't set but throwing error
118             
return standardColor;
119         }
120
121         
//no highlighting needed since connector isn't visible
122         
if (blockPosition == 2) return standardColor;
123
124         
//middle block is blank and first and second blocks match
125         
if (block1 == BlockScript.emptyBlock
126             && block0 != BlockScript.emptyBlock
127             && block0 == block2)
return highlightedColor;
128
129         
//if this block is empty then return standard color
130         
if (blockPosition == BlockScript.emptyBlock) return standardColor;
131
132         
//if the next matches
133         
if(blockPosition == 0 && block0 == block1 && block0 != BlockScript.emptyBlock) return highlightedColor;
134         
if(blockPosition == 1 && block1 == block2 && block1 != BlockScript.emptyBlock) return highlightedColor;
135
136         
//set the default color
137         
return standardColor;
138     }
139     
/*
140      * Get the number
on the block corresponding to the position parameters
141      */

142     
private int GetBlockNumber(int x, int y, int z) {
143         
return this.gameScript.getBlockNumber (x,y,z);
144     }
145
146     
public void rotateConnector(Vector3 direction) {
147         
this.rotateDirection = direction;
148         
this.rotationTotal = 0f;
149     }
150     
// Update is called once per frame
151     
void Update () {
152         
//this.renderer.enabled = this.show;
153         
//Vector3 rotationPoint = new Vector3 (3f, 4f, 3f);
154
155         
this.GetComponent<Renderer>().enabled = this.show();
156         
this.GetComponent<Renderer>().material.color = this.getConnectorColor();
157
158         
159         
if (this.rotationTotal >= 0F) {
160             
float rotateBy = 90 * Time.deltaTime / this.moveDuration;
161             Vector3 rotationPoint =
new Vector3 (3f, 4f, 3f);
162             transform.RotateAround(rotationPoint,
this.rotateDirection, rotateBy);
163             
this.rotationTotal += rotateBy;
164             
165             
if(rotationTotal >= 90) {
166                 
this.rotationTotal = -1F;
167                 
this.ResetPosition ();
168             }
169         }
170     }
171         
//transform.RotateAround(rotationPoint, Vector3.down, 20 * Time.deltaTime);
172         
//transform.RotateAround(rotationPoint, Vector3.left, 20 * Time.deltaTime);
173 }


public string axis; x,y,z

Use this for initialization

setup local versions of Game Variables

if this block is one that should not be shown

if this is on the edge then don't display

check if the neighboring block should not be shown

if this is on the edge then don't display

check if the neighboring block should not be shown

if this is on the edge then don't display

check if the neighboring block should not be shown

else { not quite sure why axis isn't set but throwing error

no highlighting needed since connector isn't visible

middle block is blank and first and second blocks match

if this block is empty then return standard color

if the next matches

set the default color

Update is called once per frame

this.renderer.enabled = this.show;

Vector3 rotationPoint = new Vector3 (3f, 4f, 3f);

transform.RotateAround(rotationPoint, Vector3.down, 20 * Time.deltaTime);

transform.RotateAround(rotationPoint, Vector3.left, 20 * Time.deltaTime);



Gõ tìm kiếm nhanh...